package com.starmark.sys.org.atomic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.starmark.core.framework.base.AbstractBaseService;
import com.starmark.core.framework.util.BeanMapper;
import com.starmark.sys.org.atomic.entity.SysOrgUserDeptRel;
import com.starmark.sys.org.atomic.mapper.SysOrgUserDeptRelMapper;
import com.starmark.sys.org.atomic.service.ISysOrgUserDeptRelService;
import com.starmark.sys.org.atomic.vo.SysOrgUserDeptRelVo;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户部门关系 服务实现类
 * </p>
 *
 * @author starmark
 * @since 2020-08-06
 */
@Service
public class SysOrgUserDeptRelServiceImpl extends AbstractBaseService<SysOrgUserDeptRelMapper, SysOrgUserDeptRel> implements ISysOrgUserDeptRelService {


    @Override
    public void buildPageSearch(Map<String, Object> params, QueryWrapper<SysOrgUserDeptRel> eWrapper) {
        if (params.get("keyword") != null && StrUtil.isNotEmpty(params.get("keyword") + "")) {
            eWrapper.like("name", params.get("keyword") + "");
        }

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUserDeptRel(String userId, List<SysOrgUserDeptRelVo> sysOrgUserDeptRels) {
        List<SysOrgUserDeptRel> userDeptRels = this.findUserDeptRels(userId);

        if (CollectionUtils.isEmpty(userDeptRels)) {
            if (CollectionUtils.isEmpty(sysOrgUserDeptRels)) {
                return;
            }
            //直接插入关系
            List<SysOrgUserDeptRel> orgUserDeptRels = sysOrgUserDeptRels.stream().map(orgUserDeptRelVo -> this.buildSysOrgUserDeptRel(userId, orgUserDeptRelVo)).collect(Collectors.toList());
            this.saveBatch(orgUserDeptRels);

        } else if (CollectionUtils.isEmpty(sysOrgUserDeptRels)) {
            //删除当前所有关系
            List<String> ids = userDeptRels.stream().map(SysOrgUserDeptRel::getId).collect(Collectors.toList());
            this.removeByIds(ids);
        } else {
            //找到对应的关系，并新增删除及变更
            List<SysOrgUserDeptRel> addList = new ArrayList<>();
            List<SysOrgUserDeptRel> updateList = new ArrayList<>();
            List<String> deleteIdsList = new ArrayList<>();
            for (SysOrgUserDeptRelVo sysOrgUserDeptRelVo : sysOrgUserDeptRels) {
                SysOrgUserDeptRel sysOrgUserDeptRel = this.findOrgUserDeptRel(userDeptRels, sysOrgUserDeptRelVo);
                if (sysOrgUserDeptRel == null) {
                    sysOrgUserDeptRel = this.buildSysOrgUserDeptRel(userId, sysOrgUserDeptRelVo);
                    addList.add(sysOrgUserDeptRel);
                } else {
                    sysOrgUserDeptRel.setMajor(sysOrgUserDeptRelVo.getMajor());
                    sysOrgUserDeptRel.setPostId(sysOrgUserDeptRelVo.getPostId());
                    updateList.add(sysOrgUserDeptRel);
                }
            }
            //获取删除数据
            for (SysOrgUserDeptRel sysOrgUserDeptRel : userDeptRels) {
                SysOrgUserDeptRelVo orgUserDeptRelVo = this.findOrgUserDeptRel(sysOrgUserDeptRels, sysOrgUserDeptRel);
                if (orgUserDeptRelVo == null) {
                    deleteIdsList.add(sysOrgUserDeptRel.getId());
                }
            }
            if (!CollectionUtils.isEmpty(addList)) {
                this.saveBatch(addList);
            }
            if (!CollectionUtils.isEmpty(updateList)) {
                this.updateBatchById(updateList);
            }
            if (!CollectionUtils.isEmpty(deleteIdsList)) {
                this.removeByIds(deleteIdsList);
            }
        }
    }

    private SysOrgUserDeptRelVo findOrgUserDeptRel(List<SysOrgUserDeptRelVo> userDeptRelVos, SysOrgUserDeptRel orgUserDeptRel) {
        return userDeptRelVos.stream().filter(sysOrgUserDeptRelVo -> this.compareUserDeptRel(orgUserDeptRel, sysOrgUserDeptRelVo))
                .findFirst().orElse(null);
    }

    private SysOrgUserDeptRel findOrgUserDeptRel(List<SysOrgUserDeptRel> userDeptRels, SysOrgUserDeptRelVo orgUserDeptRelVo) {
        return userDeptRels.stream().filter(sysOrgUserDeptRel -> this.compareUserDeptRel(sysOrgUserDeptRel, orgUserDeptRelVo))
                .findFirst().orElse(null);
    }

    private boolean compareUserDeptRel(SysOrgUserDeptRel sysOrgUserDeptRel, SysOrgUserDeptRelVo sysOrgUserDeptRelVo) {
        return Objects.equals(sysOrgUserDeptRel.getDeptId(), sysOrgUserDeptRelVo.getDeptId());
    }

    private SysOrgUserDeptRel buildSysOrgUserDeptRel(String userId, SysOrgUserDeptRelVo orgUserDeptRelVo) {
        SysOrgUserDeptRel userDeptRel = BeanMapper.map(orgUserDeptRelVo, SysOrgUserDeptRel.class);
        userDeptRel.setUserId(userId);
        return userDeptRel;
    }

    @Override
    public List<SysOrgUserDeptRel> findUserDeptRels(String userId) {
        Assert.hasText(userId, "用户ID不允许为空");
        return this.baseMapper.findUserDeptRels(userId);
    }
}
